home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_procmail.idb / usr / freeware / bin / mailstat.z / mailstat
Text File  |  1998-10-27  |  5KB  |  205 lines

  1. #! /bin/sh
  2. : &&O='cd .' || exec /bin/sh "$0" $argv:q # we're in a csh, feed myself to sh
  3. $O || exec /bin/sh "$0" "$@"          # we're in a buggy zsh
  4. #################################################################
  5. #    mailstat    shows mail-arrival statistics        #
  6. #                                #
  7. #    Parses a procmail-generated $LOGFILE and displays    #
  8. #    a summary about the messages delivered to all folders    #
  9. #    (total size, average size, nr of messages).        #
  10. #    Exit code 0 if mail arrived, 1 if no mail arrived.    #
  11. #                                #
  12. #    For help try, "mailstat -h"                #
  13. #                                #
  14. #    Customise to your heart's content, this file is only    #
  15. #    provided as a guideline.                #
  16. #                                #
  17. #    Created by S.R. van den Berg, The Netherlands        #
  18. #    This file can be freely copied for any use.        #
  19. #################################################################
  20. #$Id: mailstat,v 1.21 1995/03/20 14:51:08 berg Exp $
  21.  
  22. #    This shell script expects the following programs to be in the
  23. #    PATH (paths given here are the standard locations, your mileage
  24. #    may vary (if the programs can not be found, extend the PATH or
  25. #    put their absolute pathnames in here):
  26.  
  27. test=test        # /bin/test
  28. echo=echo        # /bin/echo
  29. expr=expr        # /bin/expr
  30. tty=tty            # /bin/tty
  31. sed=sed            # /bin/sed
  32. sort=sort        # /bin/sort
  33. awk=awk            # /usr/bin/awk
  34. cat=cat            # /bin/cat
  35. mv=mv            # /bin/mv
  36. ls=ls            # /bin/ls
  37.  
  38. PATH=/bin:/usr/bin
  39. SHELL=/bin/sh        # just in case
  40. export SHELL PATH
  41.  
  42. umask 077        # we don't allow everyone to read the tmpfiles
  43. OLDSUFFIX=.old
  44.  
  45. DEVNULL=/dev/null
  46. EX_USAGE=64
  47.  
  48. ########
  49. #    (Concatenated) flags parsing in pure, portable, structured (it
  50. #    would have been more elegant if gotos were permitted) shellscript
  51. #    language.  For added pleasure: a quick demonstration of the shell's
  52. #    quoting capabilities :-).
  53. ########
  54.  
  55. while $test $# != 0 -a a"$1" != a-- -a \
  56.  \( 0 != `$expr "X$1" : X-.` -o $# != 1 \)
  57. do
  58.   if $expr "X$1" : X-. >$DEVNULL    # structured-programming spaghetti
  59.   then
  60.      flags="$1"; shift
  61.   else
  62.      flags=-h                # force help page
  63.   fi
  64.   while flags=`$expr "X$flags" : 'X.\(.*\)'`; $test ."$flags" != .
  65.   do
  66.      case "$flags" in
  67.      k*) MSkeeplogfile=1;;
  68.      l*) MSlong=1;;
  69.      m*) MSmergerror=1;;
  70.      o*) MSoldlog=1; MSkeeplogfile=1;;
  71.      t*) MSterse=1;;
  72.      s*) MSsilent=1;;
  73.      h*|\?*) $echo 'Usage: mailstat [-klmots] [logfile]' 1>&2
  74.         $echo '    -k    keep logfile intact' 1>&2
  75.         $echo '    -l    long display format' 1>&2
  76.         $echo '    -m    merge any errors into one line' 1>&2
  77.         $echo '    -o    use the old logfile' 1>&2
  78.         $echo '    -t    terse display format' 1>&2
  79.         $echo '    -s    silent in case of no mail' 1>&2
  80.         exit $EX_USAGE;;
  81.      *) $echo 'Usage: mailstat [-klmots] [logfile]' 1>&2; exit $EX_USAGE;;
  82.      esac
  83.   done
  84. done
  85.  
  86. $test a"$1" = a-- && shift
  87.  
  88. LOGFILE="$1"
  89.  
  90. case "$LOGFILE" in
  91.   *$OLDSUFFIX) MSkeeplogfile=1; OLDLOGFILE="$LOGFILE";;
  92.   *) OLDLOGFILE="$LOGFILE$OLDSUFFIX";;
  93. esac
  94.  
  95. if test .$MSoldlog = .1
  96. then
  97.   LOGFILE="$OLDLOGFILE"
  98. fi
  99.  
  100. if $test ."$LOGFILE" != .- -a ."$LOGFILE" != .
  101. then
  102.   if $test ! -s "$LOGFILE"
  103.   then
  104.      if $test .$MSsilent = .
  105.      then
  106.     if $test -f "$LOGFILE"    # split up the following nested backquote
  107.     then            # expression, some shells (NET2) choked on it
  108.        info=`LANG= LC_TIME= $ls -l "$OLDLOGFILE"`
  109.        $echo No mail arrived since \
  110.         `$expr "X$info" : \
  111.          '.*[0-9] \(... .[^ ] .....\) [^ ]'`
  112.     else
  113.        $echo "Can't find your LOGFILE=$LOGFILE"
  114.     fi
  115.      fi
  116.      exit 1
  117.   fi
  118. else
  119.   if $test ."$LOGFILE" != .- && $tty -s
  120.   then
  121.      $echo \
  122.       "Most people don't type their own logfiles;  but, what do I care?" 1>&2
  123.      MSterse=1
  124.   fi
  125.   MSkeeplogfile=1; LOGFILE=
  126. fi
  127.  
  128. if $test .$MSkeeplogfile = .
  129. then $mv "$LOGFILE" "$OLDLOGFILE"; $cat $DEVNULL >>"$LOGFILE"
  130. else OLDLOGFILE="$LOGFILE"
  131. fi
  132.  
  133. if $test .$MSterse = .
  134. then
  135.   if $test .$MSlong = .1
  136.   then
  137.      $echo ""
  138.      $echo "  Total Average  Number Folder"
  139.      $echo "  ----- -------  ------ ------"
  140.   else
  141.      $echo ""
  142.      $echo "  Total  Number Folder"
  143.      $echo "  -----  ------ ------"
  144.   fi
  145. fi
  146.  
  147. if $test .$MSlong = .1
  148. then MSlong='"%7d %7d %7d %s\n",total,total/messages,messages,folder'
  149. else MSlong='"%7d %7d %s\n",total,messages,folder'
  150. fi
  151.  
  152. TMPF=/tmp/maillog.$$
  153.  
  154. trap "rm -f $TMPF; exit 2" 1 2 3 13 15
  155. trap "rm -f $TMPF; exit 0" 0
  156.  
  157. ########
  158. #    And now we descend into the wonderful mix of shell-quoting and
  159. #    portable awk-programming :-)
  160. ########
  161.  
  162. rm -f $TMPF
  163. $cat >$TMPF <<HERE
  164. BEGIN {
  165.     FS="\\t";
  166.   }
  167.   { if(folder!=\$1)
  168.      { if(folder!="")
  169.       printf($MSlong);
  170.        messages=0;total=0;folder=\$1;
  171.      }
  172.     ++messages;total+=\$2;
  173.   }
  174. END {
  175.     if(folder!="")
  176.        printf($MSlong);
  177.   }
  178. HERE
  179.  
  180. ########
  181. #    Only to end in a grand finale with your average sed script
  182. ########
  183.  
  184. if $test .$MSmergerror = .
  185. then
  186.   $sed    -e '/^From /d' -e '/^ [Ss][uU][bB][jJ][eE][cC][tT]:/d' \
  187.    -e '/^  Folder/s/        */    /' \
  188.    -e '/^  Folder/s/\/msg.[-0-9A-Za-z_][-0-9A-Za-z_]*    /\/    /' \
  189.    -e '/^  Folder/s/\/[0-9][0-9]*    /\/.    /' \
  190.    -e 's/^  Folder: \(.*\)/\1/' -e t -e 's/    /\\t/g' \
  191.    -e 's/^/ ## /' $OLDLOGFILE | $sort | $awk -f $TMPF -
  192. else
  193.   $sed    -e '/^From /d' -e '/^ [Ss][uU][bB][jJ][eE][cC][tT]:/d' \
  194.    -e '/^  Folder/s/        */    /' \
  195.    -e '/^  Folder/s/\/msg.[-0-9A-Za-z_][-0-9A-Za-z_]*    /\/    /' \
  196.    -e '/^  Folder/s/\/[0-9][0-9]*    /\/.    /' \
  197.    -e 's/^  Folder: \(.*\)/\1/' -e t \
  198.    -e 's/.*/ ## diagnostic messages ##/' $OLDLOGFILE | $sort | $awk -f $TMPF -
  199. fi
  200.  
  201. ########
  202. #    Nifty little script, isn't it?
  203. #    Now why didn't *you* come up with this truly trivial script? :-)
  204. ########
  205.